/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.command.organization;

import apus.command.AbstractCommand;
import apus.entity.Organization;
import apus.exception.BusinessException;
import apus.persistence.OrganizationDAO;
import apus.services.ServiceLocator;
import java.util.Arrays;
import java.util.List;

/**
 * The <code>UpdateOrganizationCommand</code> class represents command for 
 * changing information about organization.
 * @author  Maxim Vasilevsky
 * @author  Roman Dyatkovsky
 * @since APUS v0.4
 * @see AbstractCommand
 */
public class UpdateOrganizationCommand extends AbstractCommand{
   
    private final static String PARAM_ID = "Id";    
    private final static String PARAM_NAME = "Name";
    private final static String PARAM_ADDRESS = "Address";
    
    
    private final static String[] paramsNames = { PARAM_ID, 
                                                  PARAM_NAME, 
                                                  PARAM_ADDRESS };
    
    public UpdateOrganizationCommand() {
        super("Update organization");
    }
    
    @Override
    public void execute() {
        int ID;
        String stringId = params.get(PARAM_ID);
        
        try{
            ID = Integer.parseInt(stringId);
        } catch (NumberFormatException e){
            throw new BusinessException("Wrong ID format: "+stringId, e);
        }
        
        OrganizationDAO organizationDAO = ServiceLocator.getService(OrganizationDAO.class);
        Organization organization = organizationDAO.read(ID);
        
        if (organization==null){
            throw new BusinessException("Organization not found");
        }
        
        organization.setName(params.get(PARAM_NAME));
        organization.setAddress(params.get(PARAM_ADDRESS));
        organizationDAO.update(organization);
    }
    
    @Override
    public List<String> getParamNames() {
        return Arrays.asList(paramsNames);
    }
    
    
}